package server;

import java.io.*;
import java.net.*;

/* DEV LOG
 * 12/26/09 - Added this class from a friend
 *          - helping with the project
 */

public class Socket implements Runnable {

    private DatagramSocket DataSocket = null;
    private DatagramPacket DataPacket = null;
    private String PacketStr = null;
    private byte[] PacketByte = null;
    private HandleConnections[] ConnectionHandler = null;
    private boolean sRunning = false;
    private int sPort = 4000; //default

    public Socket(int setPort) {
        this.sPort = setPort;
    }

    public void run() {
        StartServer();
    }

    public void StartServer() {
        String packet[];
        try {
            PacketByte = new byte[1024];
            DataSocket = new DatagramSocket(sPort);
            DataPacket = new DatagramPacket(PacketByte, PacketByte.length);

            while (sRunning) {
                DataSocket.receive(DataPacket);
                PacketStr = new String(DataPacket.getData(), 0, DataPacket.getLength());
                packet = PacketStr.split("&");

                for (int i = 0; i < (packet.length -1); i++) {
                    if (PacketStr != null) {
                        System.out.println("Received data: " + packet[i]);
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class HandleConnections extends Thread {

    private DatagramSocket DataSocket = null;
    private DatagramPacket DataPacket = null;
    private HandleConnections[] ConnectionHandler;
    private String PacketStr = null;
    private byte[] PacketByte = null;
    private boolean sRunning = false;

    public HandleConnections(DatagramSocket DataSocket, DatagramPacket DataPacket, HandleConnections[] ConnectionHandler, byte[] PacketByte, String PacketStr, boolean sRunning) {
        this.ConnectionHandler = ConnectionHandler;
        this.sRunning = sRunning;
    }

    //@Override - not sure if it's good to override the thread.run() yet
    public void run() { }
}